﻿using System.Windows.Forms;
using CloudShot.Editor.CanvasFigures;
using CloudShot.Interfaces;
using CloudShot.Properties;
using CloudShot.Utils;

namespace CloudShot.Editor.Tools
{
  internal abstract class ObjectCreationTool<T> : ObjectTool
    where T : CanvasObject
  {
    protected T CreatedObject { get; private set; }

    protected ObjectCreationTool(IDrawingCanvas canvas, Section changeToolsSection, Section resetToolSection, Cursor cursor)
      : base(canvas, changeToolsSection, resetToolSection, cursor) { }

    /// <summary>
    ///   Left mouse is released.
    ///   New object is created and resized.
    /// </summary>
    /// <param name="e"></param>
    public override void OnMouseUp(MouseEventArgs e)
    {
      base.OnMouseUp(e);

      if (CreatedObject != null)
      {
        using (new AutomaticCanvasRefresher(Canvas, CreatedObject.GetBoundingBox))
        {
          CreatedObject.IsShowSize = false;
          CreatedObject.Normalize();  
        }
      }
    }

    protected virtual string GetHintEnterText()
    {
      return HintResources.ObjectTool_Enter;
    }

    public override void Enter()
    {
      base.Enter();
      Canvas.ShowHint(GetHintEnterText());
    }

    
    public override void Leave()
    {
      base.Leave();

      if (CreatedObject != null)
        CreatedObject.IsShowSize = false;

      CreatedObject = null;
      Canvas.HideHint();
    }

    /// <summary>
    ///   Add new object to draw area.
    ///   Function is called when user left-clicks draw area,
    ///   and one of ToolObject-derived tools is active.
    /// </summary>
    /// <param name="canvas"></param>
    /// <param name="newObject"></param>
    protected void AddNewObject(IDrawingCanvas canvas, T newObject)
    {
      CreatedObject = newObject;
      CreatedObject.IsShowSize = true;

      canvas.GraphicsList.DeselectAll();
      newObject.IsSelected = true;
      canvas.GraphicsList.Add(newObject);
      canvas.CaptureMouse = true;
      canvas.ShowHint(HintTextHelper.GetResizeHint(newObject));
      canvas.RefreshCanvas(); //to redraw another object selection state
    }
  }
}